﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using System.Collections.ObjectModel;
using ARR.UI.ExcelImporterService;
using DDF.Entity;
using ARR.UI.SpacesService;

namespace ARR.UI.Models
{

    public interface ISpacesImporterModel : IModelBase
    {
        List<ImporterResult> Results { get; set; }
        
        ObservableCollection<TempSpace> TempSpaces { get; set; }

        ObservableCollection<TempSpace> TempSpacesCache { get; set; }

        void SaveTempSpaces();
    }

    public class SpacesImporterModel : ModelBase, ISpacesImporterModel
    {
        public SpacesImporterModel()
        {
            this.Results = new List<ImporterResult>();
            this.TempSpaces = new ObservableCollection<TempSpace>();
            this.TempSpacesCache = new ObservableCollection<TempSpace>();
        }

        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempSpace> TempSpaces { get; set; }

        public ObservableCollection<TempSpace> TempSpacesCache { get; set; }


        public void SaveTempSpaces()
        {
            var toSave = TempSpacesCache.Where(x => x.GetEntityState() != EntityState.Original);

            if (toSave.Any())
            {
                SpacesServiceClient service = new SpacesServiceClient();

                try
                {
                    service.SaveTempSpaces(toSave.ToList());
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }

            }
        }
    }


    public class SpacesImporterModelMock : MockModelBase, ISpacesImporterModel
    {
        public SpacesImporterModelMock()
        {
            this.Results = new List<ImporterResult>();
            this.TempSpaces = new ObservableCollection<TempSpace>();
            this.TempSpacesCache = new ObservableCollection<TempSpace>();
        }

        public List<ImporterResult> Results { get; set; }

        public ObservableCollection<TempSpace> TempSpaces { get; set; }

        public ObservableCollection<TempSpace> TempSpacesCache { get; set; }
        

        public void SaveTempSpaces()
        {

        }
    }
}
